package com.ssy.bus.controller;

import com.ssy.bus.pojo.Order;
import com.ssy.bus.service.IDrugInfoService;
import com.ssy.bus.service.IOrderService;
import com.ssy.bus.vo.DrugInfoVo;
import com.ssy.bus.vo.OrderVo;
import com.ssy.common.utils.PageResult;
import com.ssy.common.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author ssy
 */
@RestController
@RequestMapping("/make")
public class MakeController {

    @Autowired
    private IDrugInfoService drugInfoService;

    @Autowired
    private IOrderService orderService;

    private String number;

    /**
     * 获取一个订单号
     *
     * @return data
     */
    @RequestMapping("/getBillNumber")
    public Result getBillNumber() {
        //使用时间戳生成订单号
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        number = sdf.format(date).substring(2);
        return new Result(number);
    }

    /**
     * 加载弹出层的数据表格
     *
     * @param drugInfoVo 药品信息Vo
     * @return data
     */
    @GetMapping("/loadMakeDrugInfo")
    public PageResult loadMakeDrugInfo(DrugInfoVo drugInfoVo) {
        return this.drugInfoService.loadMakeDrugInfoPage(drugInfoVo);
    }

    /**
     * 订单的分页列表
     *
     * @return data
     */
    @GetMapping("/loadAllOrderPage")
    public PageResult loadAllOrderPage(OrderVo orderVo) {
        orderVo.setPurId(number);
        return this.orderService.loadAllOrderByPage(orderVo);
    }

    /**
     * 第一次生成订单
     *
     * @param order order
     * @return data
     */
    @PostMapping("/addMakeDrugInfoOne")
    public Result addMakeDrugInfoOne(Order order) {
        return this.orderService.addMakeDrugInfoOne(order);
    }

    /**
     * 药品的批量设置
     *
     * @param order order
     * @return data
     */
    @PostMapping("/addMakeDrugInfoTwo")
    public Result addMakeDrugInfoTwo(Order order) {
        return this.orderService.addMakeDrugInfoTwo(order);
    }


    /**
     * 删除订单列表的药品
     *
     * @param id    药品ID
     * @param purId 订单ID
     * @return data
     */
    @DeleteMapping("/deleteMakeDrugInfo/{id}/{purId}")
    public Result deleteMakeDrugInfo(@PathVariable Integer id, @PathVariable String purId) {
        return this.orderService.deleteDrugInfoByDrugId(id, purId);
    }

    /**
     * 单元格编辑的修改
     *
     * @param order order
     * @return data
     */
    @PostMapping("/updateMakeDrugInfo")
    public Result updateMakeDrugInfo(Order order) {
        return this.orderService.updateDrugInfoByDrugId(order);
    }


    /**
     * 加载总的批发额
     *
     * @param purId 订单ID
     * @return data
     */
    @GetMapping("/loadSumWholesaleAmount")
    public Result loadSumWholesaleAmount(String purId) {
        return this.orderService.selectCountWholesaleAmount(purId);
    }

    /**
     * 加载修改页面重载的列表
     *
     * @param purId 订单ID
     * @return data
     */
    @GetMapping("/loadUpdateOrderByPage/{purId}")
    public PageResult loadUpdateOrderByPage(OrderVo orderVo, @PathVariable String purId) {
        orderVo.setPurId(purId);
        return this.orderService.loadAllOrderByPage(orderVo);
    }

}
